﻿$(document).ready(function () {
    pageload();
    $("#btnshow_tpl").click(medicine.showadd);
});
var methods = new GetDataJSON();
function pageload() {
    medicine.init();
    birthday.init();
    holiday.init();
}
// var takepills = {
//     showadd: function () { layerShow('takepills'); },
//     showedit: function () { layerShow('takepills'); },
//     showdelete: function () { layerShow('takepillsdelete'); },
//     save: function () {
//         var model = this.getmodel();
//         console.log(model);
//     },
//     del: function () { },
//     getmodel: function () {
//         var model = new Object();
//         model.id = $("#takepills_id").val();
//         model.hours = $("#takepills_hours").val();
//         model.minute = $("#takepills_minute").val();
//         return model;
//     }
// }
var medicine = {
    medicineList: null,
    medicineModel: null,
    medicineDeleteModel: null,
    init: function () {
        this.medicineList = new Vue({ el: '#medicine_List', data: { items: null } });
        this.medicineModel = new Vue({ el: '#medicine', data: { model: null } });
        this.medicineDeleteModel = new Vue({ el: '#medicinedelete', data: { model: null } });
        this.listload();
    },
    listload: function () {
        function callback(result) {
          // console.log(result);
            if (!checkStatus(result.Status)) {
                $("#medicine_List_Error").show().html("<span class='column3'>" + result.Message + "</span>");

            } else {
                $("#medicine_List_Error").hide();
                var onoff = result.Data[0][4];
            }
            if( onoff == '开'){
            $('#medicineonoff').find("span").css({ left: 37 });
            $('#medicineonoff').find("input[type=checkbox]").attr("checked", true);
          }else{
            $('#medicineonoff').prev().html("关");
            $('#medicineonoff').find("input[type=checkbox]").attr("checked", false);
            $('#medicineonoff').find("span").css({ left: 0 });
          }

            $('#medicineonoff').prev().html(onoff);

            medicine.medicineList.items = result.Data;
        }
        methods.MedicineMethods.SelectMedicine(callback);
    },
    showadd: function (_id) {
        // this.medicineModel.model = null;
        // var model = this.getcurrentModel(_id);
        // model.h = '02';
        $('#medicine_title').attr('type','text');
        $('#medicines_title').attr('type','hidden');
        var model = [];
        model.h = '01';
        model.m = '00';
        this.medicineModel.model = model;
        layerShow('medicine');
    },
    showedit: function (_id) {
      $('#medicine_title').attr('type','hidden');
      $('#medicines_title').attr('type','text');
        var model = this.getcurrentModel(_id);
        var h = model['starttime'].substring(0,2);
        var m = model['starttime'].substring(3,5);
        model['h'] = h;
        model['m']= m;
        // console.log(model);
        // alert(123);
        this.medicineModel.model = model;
        layerShow('medicine');
    },
    showkaiguan:function (_id){
        layerShow('medicinekaiguan');
    },
    showdelete: function (_id) {
        var model = this.getcurrentModel(_id);
        this.medicineDeleteModel.model = model;
        layerShow('medicinedelete');
    },
    showonoff: function () {
      var allcookies = document.cookie;
      var openid = allcookies.substring(5);
      var did = document.getElementById('honoff').value;
      // alert(did);
      var onoff = $('#medicineonoff').prev().html();
      if (onoff == '关'){
        $('#medicineonoff').find("input[type=checkbox]").attr("checked", false);
        $('#medicineonoff').find("span").css({ left: 0 });
        onoff = $('#medicineonoff').prev().html('关');
        onoff = '开';
      }else if ( onoff == '开' ){
         $('#medicineonoff').find("input[type=checkbox]").attr("checked", true);
         $('#medicineonoff').find("span").css({ left: 37 });
         onoff = $('#medicineonoff').prev().html('开');
         onoff = '关';

       }
      //  layer.closeAll();
      //  medicine.listload();
      //  alert(onoff);
      var model = [];

       model.did = did;
       model.onoff = onoff;
       model.openid = openid;
       methods.MedicineMethods.OnoffMedicine(model);
       layer.closeAll();
       medicine.listload();


    },
    save: function () {
      // console.log($("#medicine_Year").val());
        // var title = document.getElementById("medicine_title").value;
        var model = this.medicineModel.model;


        // var year = $("#medicine_Year").val();
        var hours = $("#medicine_hours").val();
        var minute = $("#medicine_minute").val();

        model.starttime = hours+':'+minute;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                medicine.listload();
                layer.closeAll();
            }
        }
        var onoff = $('#medicineonoff').prev().html();
        // alert(onoff);
         if (onoff == '开'){
            onoff = '开';
         }else{
            onoff = '关';
         }

         model.onoff = onoff;
        model.title  = model[0];

        // alert(model.title);
        if (model == null || model.id == null || model.id == undefined) {
          var title = document.getElementById("medicine_title").value;
          model.title = title;
          // console.log(model);
          if (model.title== null) {
              alert("请填写信息");
              return;
          }
            methods.MedicineMethods.AddMedicine(model, callback);
        } else {
          var title = document.getElementById("medicines_title").value;
          // console.log(title);
          model.title = title;
          if (title.length == 0) {
              alert("请填写信息");
              return;
          }
            methods.MedicineMethods.ChangeMedicine(model, callback);
        }
    },
    del: function () {
        var model = this.medicineDeleteModel.model;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                medicine.listload();
                layer.closeAll();
            }
        }
    methods.MedicineMethods.DelHoliMedicine(model, callback);

    },
    getmodel: function () {
        var model = new Object();
        model.id = $("#takepills_id").val();
        model.hours = $("#takepills_hours").val();
        model.minute = $("#takepills_minute").val();
        return model;
    },
    getcurrentModel: function (_id) {
        var model = new Object();
        var thislist = new Object();
        var list = this.medicineList.items;
        for (var i = 0; i < list.length; i++) {
            if (list[i][0] == _id) {
                thislist = list[i];
                break;
            }
        }
        // console.log(thislist[2]);
        model.id = thislist[0];
        model.title = thislist[1];
        model.starttime = thislist[2];
        model.contt = thislist[3];
        model.onoff = thislist[4];
        // console.log(model);
        return model;
    }
}

var birthday = {
    birthdayList: null,
    birthdayModel: null,
    birthdayDeleteModel: null,
    init: function () {
        this.birthdayList = new Vue({ el: '#birthday_List', data: { items: null } });
        this.birthdayModel = new Vue({ el: '#birthday', data: { model: null } });
        this.birthdayDeleteModel = new Vue({ el: '#birthdaydelete', data: { model: null } });
        this.listload();
    },
    listload: function () {
        function callback(result) {
            if (!checkStatus(result.Status)) {
                $("#birthday_List_Error").show().html("<span class='column3'>" + result.Message + "</span>");
            } else {
                $("#birthday_List_Error").hide();
                var onoff = result.Data[0][4];
            }
            // console.log(result.Data);
              if( onoff == '开'){
              $('#birthdayonoff').find("span").css({ left: 37 });
              $('#birthdayonoff').find("input[type=checkbox]").attr("checked", true);
            }else{
              $('#birthdayonoff').prev().html("关");
              $('#birthdayonoff').find("input[type=checkbox]").attr("checked", false);
              $('#birthdayonoff').find("span").css({ left: 0 });
            }
            $('#birthdayonoff').prev().html(onoff);
            birthday.birthdayList.items = result.Data;
        }
        methods.BirthdayMethods.SelectBirthday(callback);
    },
    showadd: function (_id) {
        // this.birthdayModel.model = null;
        // alert(1);
        $('#birthday_title').attr('type','text');
        $('#birthdays_title').attr('type','hidden');
        var model = [];
        model.h = '01';
        model.m = '00';
        this.birthdayModel.model = model;
        layerShow('birthday');
    },
    showedit: function (_id) {
      $('#birthday_title').attr('type','hidden');
      $('#birthdays_title').attr('type','text');
        var model = this.getcurrentModel(_id);
        var h = model['starttime'].substring(0,2);
        var m = model['starttime'].substring(3,5);
        model['h'] = h;
        model['m']= m;
        this.birthdayModel.model = model;
        layerShow('birthday');
    },
    showkaiguan:function (_id){
        layerShow('birthdaykaiguan');
    },
    showdelete: function (_id) {
        var model = this.getcurrentModel(_id);
        this.birthdayDeleteModel.model = model;
        layerShow('birthdaydelete');
    },

    // showonoff: function () {
    //   var allcookies = document.cookie;
    //   var openid = allcookies.substring(5);
    //   var did = document.getElementById('honoff').value;
    //   // alert(did);
    //   var onoff = $('#birthdayonoff').prev().html();
    //   if (onoff == '关'){
    //       onoff = '开';
    //    }else{
    //       onoff = '关';
    //    }
    //   //  alert(onoff);
    //   var model = [];
    //
    //    model.did = did;
    //    model.onoff = onoff;
    //    model.openid = openid;
    //    methods.BirthdayMethods.OnoffBirthday(model);
    //
    // },
    showonoff: function () {

      var allcookies = document.cookie;
      var openid = allcookies.substring(5);
      var did = document.getElementById('honoff').value;
      // alert(did);
      var onoff = $('#birthdayonoff').prev().html();
      if (onoff == '关'){
        $('#birthdayonoff').find("input[type=checkbox]").attr("checked", false);
        $('#birthdayonoff').find("span").css({ left: 0 });
        onoff = $('#birthdayonoff').prev().html('关');
        onoff = '开';

      }else if ( onoff == '开' ){
         $('#birthdayonoff').find("input[type=checkbox]").attr("checked", true);
         $('#birthdayonoff').find("span").css({ left: 37 });
         onoff = $('#birthdayonoff').prev().html('开');
          onoff = '关';

       }
      //  layer.closeAll();
      //  birthday.listload();
      //  alert(onoff);
      var model = [];

       model.did = did;
       model.onoff = onoff;
       model.openid = openid;
       methods.BirthdayMethods.OnoffBirthday(model);
       layer.closeAll();
       birthday.listload();
    },
    save: function () {
      // console.log($("#birthday_Year").val());
        var model = this.birthdayModel.model;

        // var year = $("#birthday_Year").val();
        var hours = $("#birthday_hours").val();
        var minute = $("#birthday_minute").val();

        model.starttime =hours+'-'+minute;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                birthday.listload();
                layer.closeAll();
            }
        }
        var onoff = $('#birthdayonoff').prev().html();
        // alert(onoff);
         if (onoff == '开'){
            onoff = '开';
         }else{
            onoff = '关';
         }
        model.onoff = onoff;

        if (model == null || model.id == null || model.id == undefined) {
            var title = document.getElementById("birthday_title").value;
            model.title = title;
          if (model.title== null) {
              alert("请填写信息");
              return;
          }
            methods.BirthdayMethods.AddBirthday(model, callback);
        } else {
          var title = document.getElementById("birthdays_title").value;
          model.title = title;
          if (title.length== 0) {
              alert("请填写信息");
              return;
          }
            methods.BirthdayMethods.ChangeBirthday(model, callback);
        }
    },
    del: function () {
        var model = this.birthdayDeleteModel.model;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                birthday.listload();
                layer.closeAll();
            }
        }
        methods.BirthdayMethods.DelHoliBirthday(model, callback);
    },
    getmodel: function () {
        var model = new Object();
        model.id = $("#takepills_id").val();
        model.hours = $("#takepills_hours").val();
        model.minute = $("#takepills_minute").val();
        return model;
    },
    getcurrentModel: function (_id) {
        var model = new Object();
        var thislist = new Object();
        var list = this.birthdayList.items;
        for (var i = 0; i < list.length; i++) {
            if (list[i][0] == _id) {
                thislist = list[i];
                break;
            }
        }
        // console.log(thislist[2]);
        model.id = thislist[0];
        model.title = thislist[1];
        model.starttime = thislist[2];
        model.contt = thislist[3];
        model.onoff = thislist[4];
        // console.log(model);
        return model;
    }
}


var holiday = {
    holidayList: null,
    holidayModel: null,
    holidayDeleteModel: null,
    init: function () {
        this.holidayList = new Vue({ el: '#holiday_List', data: { items: null } });
        this.holidayModel = new Vue({ el: '#holiday', data: { model: null } });
        this.holidayDeleteModel = new Vue({ el: '#holidaydelete', data: { model: null } });
        this.listload();
    },
    listload: function () {
        function callback(result) {
            if (!checkStatus(result.Status)) {
                $("#holiday_List_Error").show().html("<span class='column3'>" + result.Message + "</span>");
            } else {
                $("#holiday_List_Error").hide();
                var onoff = result.Data[0][4];
            }
            if( onoff == '开'){
              $('#holidayonoff').find("input[type=checkbox]").attr("checked", true);
              $('#holidayonoff').find("span").css({ left: 37 });
            }else {
              $('#holidayonoff').prev().html("关");
              $('#holidayonoff').find("input[type=checkbox]").attr("checked", false);
              $('#holidayonoff').find("span").css({ left: 0 });
            }
            $('#holidayonoff').prev().html(onoff);
            holiday.holidayList.items = result.Data;
        }
        methods.HolidayMethods.SelectHoliday(callback);
    },
    showadd: function () {
      $('#holiday_title').attr('type','text');
      $('#holidays_title').attr('type','hidden');
        var model = [];
        model.h = '01';
        model.m = '00';
        this.holidayModel.model = model;
        layerShow('holiday');
    },
    showedit: function (_id) {
      $('#holiday_title').attr('type','hidden');
      $('#holidays_title').attr('type','text');
        var model = this.getcurrentModel(_id);
        var h = model['starttime'].substring(0,2);
        var m = model['starttime'].substring(3,5);
        model['h'] = h;
        model['m']= m;
        this.holidayModel.model = model;
        layerShow('holiday');
    },
    showkaiguan:function (_id){
        layerShow('holidaykaiguan');
    },
    showdelete: function (_id) {
        var model = this.getcurrentModel(_id);
        this.holidayDeleteModel.model = model;
        layerShow('holidaydelete');
    },
    showonoff: function () {

      var allcookies = document.cookie;
      var openid = allcookies.substring(5);
      var did = document.getElementById('honoff').value;
      // alert(did);
      var onoff = $('#holidayonoff').prev().html();
      if (onoff == '关'){
        $('#holidayonoff').find("input[type=checkbox]").attr("checked", false);
        $('#holidayonoff').find("span").css({ left: 0 });
        onoff = $('#holidayonoff').prev().html('关');
        onoff = '开';

      }else if ( onoff == '开' ){
         $('#holidayonoff').find("input[type=checkbox]").attr("checked", true);
         $('#holidayonoff').find("span").css({ left: 37 });
         onoff = $('#holidayonoff').prev().html('开');
          onoff = '关';

       }
      //  layer.closeAll();
      //  holiday.listload();
      //  alert(onoff);
      var model = [];

       model.did = did;
       model.onoff = onoff;
       model.openid = openid;
       methods.HolidayMethods.OnoffHoliday(model);
       layer.closeAll();
       holiday.listload();
    },

    save: function () {
      // console.log($("#birthday_Year").val());
        var model = this.holidayModel.model;


        var hours = $("#holiday_hours").val();
        var minute = $("#holiday_minute").val();

        model.starttime = hours+'-'+minute;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                holiday.listload();
                layer.closeAll();
            }
        }
        var onoff = $('#holidayonoff').prev().html();
        // alert(onoff);
         if (onoff == '开'){
            onoff = '开';
         }else{
            onoff = '关';
         }
         model.onoff = onoff;

        // alert(model[0]);

        if (model == null || model.id == null || model.id == undefined) {
          var title = document.getElementById("holiday_title").value;
          model.title = title;
          if (model.title== null) {
              alert("请填写信息");
              return;
          }
            methods.HolidayMethods.AddHoliday(model, callback);
        } else {
          var title = document.getElementById("holidays_title").value;
          model.title = title;
          if (title.length== 0) {
              alert("请填写信息");
              return;
          }
            methods.HolidayMethods.ChangeHoliday(model, callback);
        }
    },
    del: function () {
        var model = this.holidayDeleteModel.model;
        function callback(result) {
            if (!checkStatus(result.Status)) {
                alert(result.Message);
            } else {
                holiday.listload();
                layer.closeAll();
            }
        }
        methods.HolidayMethods.DelHoliBirthday(model, callback);
    },
    getmodel: function () {
        var model = new Object();
        model.id = $("#takepills_id").val();
        model.hours = $("#takepills_hours").val();
        model.minute = $("#takepills_minute").val();
        return model;
    },
    getcurrentModel: function (_id) {
        var model = new Object();
        var thislist = new Object();
        var list = this.holidayList.items;
        for (var i = 0; i < list.length; i++) {
            if (list[i][0] == _id) {
                thislist = list[i];
                break;
            }
        }
        // console.log(thislist[2]);
        model.id = thislist[0];
        model.title = thislist[1];
        model.starttime = thislist[2];
        model.contt = thislist[3];
        model.onoff = thislist[4];
        // console.log(model);
        return model;
    }
}

// $("#medicineonoff").click(function () {
//     var checked = $(this).find("input[type=checkbox]").attr("checked");
//     if (checked) {
//         $(this).prev().html("关");
//         $(this).find("input[type=checkbox]").attr("checked", false);
//         $(this).find("span").css({ left: 0 });
//     } else {
//         $(this).prev().html("开");
//         $(this).find("input[type=checkbox]").attr("checked", true);
//         $(this).find("span").css({ left: 37 });
//     }
// });
// $("#birthdayonoff").click(function () {
//
//     var checked = $(this).find("input[type=checkbox]").attr("checked");
//     if (checked) {
//         $(this).prev().html("关");
//         $(this).find("input[type=checkbox]").attr("checked", false);
//         $(this).find("span").css({ left: 0 });
//     } else {
//         $(this).prev().html("开");
//         $(this).find("input[type=checkbox]").attr("checked", true);
//         $(this).find("span").css({ left: 37 });
//     }
// });
// $("#holidayonoff").click(function () {
//     var checked = $(this).find("input[type=checkbox]").attr("checked");
//     if (checked) {
//         $(this).prev().html("关");
//         $(this).find("input[type=checkbox]").attr("checked", false);
//         $(this).find("span").css({ left: 0 });
//     } else {
//         $(this).prev().html("开");
//         $(this).find("input[type=checkbox]").attr("checked", true);
//         $(this).find("span").css({ left: 37 });
//     }
// });
